import java.awt.Rectangle;

import com.speljohan.rsbot.script.Random;

public class Certer extends Random {
	int[] models = { 2807, 8828, 8829, 8832, 8833, 8834, 8835, 8836, 8837 };
	String[] itemNames = { "bowl", "battleaxe", "fish", "shield", "helmet",
			"ring", "shears", "sword", "spade" };
	String currentItem;
	public boolean solving = false;

	@Override
	public boolean activateCondition() {
		try {
			return inCerter() && !solving;
		} catch (final Exception e) {
		}
		return false;
	}

	@Override
	public String getAuthor() {
		return "Nightmares18";
	}

	@Override
	public double getVersion() {
		return 1.0;
	}

	public boolean inCerter() {
		try {
			return getNearestNPCByName("Niles") != null
					|| getNearestNPCByName("Miles") != null
					|| getNearestNPCByName("Giles") != null;
		} catch (final Exception e) {
		}
		return false;
	}

	public boolean isChooseInterfaceUp() {
		try {
			return this.getInterface(184, 0).isValid();
		} catch (final Exception e) {
		}
		return false;
	}

	@Override
	public int loop() {
		try {
			solving = true;
			while (inCerter()) {
				if (isChooseInterfaceUp()) {
					final int modelID = this.getInterface(184, 8)
							.getComponents()[3].getModelID();
					currentItem = null;
					log("Choosing right thing");
					for (int i = 0; i < models.length; i++) {
						if (models[i] == modelID) {
							currentItem = itemNames[i];
						}
					}
					log("We are clicking " + currentItem);
					final Rectangle main = new Rectangle(133, 53, 44, 5);
					for (int j = 0; j < 3; j++) {
						if (this.getInterface(184, 8).getComponents()[j]
								.getText().contains(currentItem)) {
							this.clickMouse(main.x, main.y + j * 30,
									main.width, main.height, true);
							wait(1500, 2000);
						}
					}
					log("Congratz we should have clicked the right item. ");
				} else {
					while (canContinue()) {
						clickContinue();
						wait(random(1000, 1100));
					}
					if (inCerter() && !isChooseInterfaceUp()
							&& getContinueChildInterface() == null) {
						log("I think we have done the random it is time to exit. ");
						atObject(this.findObject(11368), "Enter");
						while (getMyPlayer().isMoving()) {
							wait(random(1000, 11000));
						}
						wait(random(4000, 5000));
					}
				}
				wait(random(200, 400));
			}
			solving = false;
		} catch (final Exception e) {
		}
		return -1;
	}
}
